package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.entity.CommonBean;
import com.beiing.tianshuai.tianshuai.mine.entity.BindingBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import java.util.Calendar;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * Created by Jinni on 2017/9/1 0001.
 */

public class BindingModel implements BindingModelImpl {

    private static final String TAG = "BindingModel";
    private OnRequestListener mListener;

    public BindingModel(OnRequestListener listener) {
        mListener = listener;
    }

    /**
     * 绑定接口
     * @param platform
     * @param unionId   唯一标识
     * @param nickName  用户昵称
     * @param avatarUrl 头像地址
     * @param sign      签名
     * @param tel       手机号码
     * @param code      验证码
     * @param pass      密码
     */
    @Override
    public void submitUserInfo(String platform, String unionId, String nickName, String avatarUrl, String sign, String tel, String code, String pass) {
        //构建body
        RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("unionid", unionId)
                .addFormDataPart("nickname", nickName)
                .addFormDataPart("headimgurl", avatarUrl)
                .addFormDataPart("tel", tel)
                .addFormDataPart("pass", pass)
                .addFormDataPart("type", platform)
                .addFormDataPart("code", code)
                .addFormDataPart("addtime", String.valueOf(Calendar.getInstance().getTimeInMillis()))
                .build();
        Observable<BindingBean> observable = DataManager.getBindingResult(requestBody);
        Observable<BindingBean> subscribeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        subscribeOn.subscribe(new DisposableObserver<BindingBean>() {

            @Override
            public void onNext(BindingBean body) {
                if (mListener != null) {
                    mListener.onSuccess(body);
                }
            }

            @Override
            public void onError( Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getSignUpCode(String number) {
        Observable<CommonBean> singUp = DataManager.getSignUpCode(
                number
        );
        Observable<CommonBean> signUpBeanObservable = singUp.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<CommonBean>() {

            @Override
            public void onNext(@NonNull CommonBean signUpBean) {
                if (mListener != null) {
                    mListener.getSafeCodeSuccess(signUpBean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i(TAG, "------------------ onCompleted ------------------");
            }
        });
    }

    @Override
    public void isAlreadySignUp(String number) {
        Observable<CommonBean> singUp = DataManager.isAlreadySignUp(
                number
        );
        Observable<CommonBean> signUpBeanObservable = singUp.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<CommonBean>() {

            @Override
            public void onNext(@NonNull CommonBean signUpBean) {
                if (mListener != null) {
                    mListener.isAlreadySignUp(signUpBean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i(TAG, "------------------ onCompleted ------------------");
            }
        });
    }

    public interface OnRequestListener {
        void onSuccess(BindingBean body);

        void onError(Throwable throwable);

        void getSafeCodeSuccess(CommonBean bean);

        void isAlreadySignUp(CommonBean bean);
    }
}
